<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
	      :row-style="rowStyle">
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="notice">通知</el-button>
          <el-button type="primary" size="small" @click="handleExport">导出</el-button>
        </template>
        <template slot="menu" slot-scope="{row, index}">
          <el-button type="text" size="small"
            icon="el-icon-edit"
            v-if="row.backStatus=='0'"
            v-permission="permissionList.editBtn"
            @click="rowEdit(row, index)">编 辑
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  noticeBatch,
  sendBatch,
  cancelBatch,
  delBatch,
  putObj
} from "@/api/subwayepm/swexticketoperationchezhan";
import {} from "@/const/crud/subwayepm/swexticketoperationchezhan";
import moment from 'moment';

export default {
  name: "swexticketoperationchezhan",
  mixins: [baseCrud('subwayepm/swexticketoperationchezhan')],
  data() {
    return {
      searchForm: {
        backStatus: '0'
      }
    }
  },
  created() {
    this.epm_swexticketoperation_sendBatch = this.permissions[
      "epm_swexticketoperation_sendBatch"
    ];
    this.epm_swexticketoperation_cancelBatch = this.permissions[
      "epm_swexticketoperation_cancelBatch"
    ];
    this.epm_swexticketoperation_dels = this.permissions[
      "epm_swexticketoperation_dels"
    ];
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_edit,
          false
        )
      };
    }
  },
  methods: {
    //颜色特殊标识
    rowStyle({ row, column, rowIndex }) {
      if (moment(row.noticeTime).add(Number(row.backTimeLimit),'days')<moment()&&row.backStatus=='0') {
        return {
          backgroundColor: "#eee",
          color: "red",
        };
      }
    },
    notice() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if(table.backStatus=='1'){
          this.$message.error("已反馈数据，请勿重复反馈");
          return false;
        }
        ids.push(table);
      }
      noticeBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("确认成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    send() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table);
      }
      sendBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("确认成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    cancel() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table);
      }
      cancelBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("确认成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    del() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.noticeStatus != 0) {
          this.$message.error("请检查所选数据，只有未通知状态的数据可以删除");
          return false;
        }
        if (table.noticeStatus == 0) {
          ids.push(table.id);
        }
      }
      delBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("确认成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    rowUpdate: function (row, done, loading) {
        if (JSON.stringify(row.file) === '{}' || row.file==null  || row.file==""){
            row.file="";	    
        }	
        putObj(row).then(data => {
          this.$message.success('修改成功')
          this.getList(this.page)
          done()
        }).catch(() => {
          loading()
        })
      },
  }
}
</script>
